﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessHandler;
using EasyCode.Utility;
public partial class Page_AddNewCategory : System.Web.UI.Page
{
    private CategoryController categoryController;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckRoles.CanInsert())
        {
            Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
        }
        if (!IsPostBack)
        {
            this.GetCategory();
        }
    }
    private void GetCategory()
    {
        categoryController = CategoryController.Instance;
        Dictionary<int, string> myDic = new Dictionary<int, string>();
        int iCount = 0;
        Dictionary<int, string> objListCat = BuilTreeList(ref myDic, ref iCount, categoryController.GetCategoryforInsertNews());
        this.DropDownList_Category.DataSource = objListCat;
        this.DropDownList_Category.DataTextField = "Value";
        this.DropDownList_Category.DataValueField = "Key";
        this.DropDownList_Category.DataBind();
        this.DropDownList_Category.Items.Insert(0, new ListItem("--Root--", "0"));
        //if (disableList.Length>0)
        //{
        //    int len=disableList.Length;
        //    foreach (ListItem item in DropDownList_Category.Items)
        //    {
        //        for (int i=0; i<len; i++)
        //        {
        //            if (item.Value==this.disableList[i].ToString())
        //            {
        //                item.Attributes.Add("disabled", "disabled");
        //                break;
        //            }
        //        }
        //    }
        //}
    }
    protected Dictionary<int, string> BuilTreeList(ref Dictionary<int, string> myDic, ref int iCount, List<Category> myList)
    {
        this.categoryController = CategoryController.Instance;
        List<Category> allChild = categoryController.GetAllChildCat();
        foreach (Category item in myList)
        {
            int addKey = Protector.Int(item.CategoryID);
            string addValue = GetStringName(0) + HttpUtility.HtmlDecode(item.CategoryName);
            myDic.Add(addKey, addValue);

            List<Category> objListChild = (from o in allChild
                                           where o.CategoryParentID == addKey
                                           select o).ToList();
            if (objListChild.Count > 0)
            {
                //this.disableList=disableList+addKey.ToString();
                iCount++;
                BuilTreeListChild(allChild, addKey, ref myDic, ref iCount);
                iCount--;
            }
        }
        if (myDic != null && myList.Count > 0)
        {
            return myDic;
        }
        else
        {
            return new Dictionary<int, string>();
        }
    }
    protected void BuilTreeListChild(List<Category> myListOriginal, int parentID, ref Dictionary<int, string> myDic, ref int iCount)
    {
        List<Category> myList = (from o in myListOriginal
                                 where o.CategoryParentID == parentID
                                 select o).ToList();

        if (myList != null)
        {
            foreach (Category item in myList)
            {
                int addKey = Protector.Int(item.CategoryID);
                string addValue = GetStringName(iCount) + HttpUtility.HtmlDecode(item.CategoryName);
                myDic.Add(addKey, addValue);
                List<Category> objListChild = (from o in myListOriginal
                                               where o.CategoryID == addKey
                                               select o).ToList();
                if (objListChild.Count > 0)
                {
                    iCount++;
                    BuilTreeListChild(myListOriginal, addKey, ref myDic, ref iCount);
                    iCount--;
                }
            }
        }
    }
    protected string GetStringName(int levelID)
    {
        string abc = "";
        for (int o = 0; o < levelID; o++)
        {
            abc += "|-- ";
        }
        return abc;
    }

    protected void Button_Save_Click(object sender, EventArgs e)
    {
        Category item = new Category();
        item.CategoryName = HttpUtility.HtmlEncode(this.TextBox_CategoryName.Text.Trim());
        item.CategoryDescription = HttpUtility.HtmlEncode(this.TextBox_Description.Text.Trim());
        item.IsAddNews = true;
        if (this.DropDownList_Category.SelectedIndex > 0)
        {
            item.CategoryParentID = Protector.Int(this.DropDownList_Category.SelectedValue, true);
        }
        item.CategoryOrder = Protector.Int(this.TextBox_Order.Text, true);
        this.categoryController = CategoryController.Instance;
        if (categoryController.AddNewCategory(item))
        {
            this.Message_Alert.SetSuccess(string.Format(Resources.Category.AddSuccess, item.CategoryName));
            this.ResetAll();
        }
        else
        {
            this.Message_Alert.SetSuccess(Resources.Category.AddFail);
        }
    }
    private void ResetAll()
    {
        this.TextBox_CategoryName.Text = string.Empty;
        this.TextBox_Description.Text = string.Empty;
        this.TextBox_Order.Text = string.Empty;
        this.GetCategory();
    }
}